<?php
/**
 * Created by PhpStorm.
 * 楼小二用户分组用户
 * User: caicm
 * Date: 2017/2/18
 * Time: 9:18
 */

namespace SDXApi\Model;
use Think\Model;

class LouxiaoerGroupUserModel extends Model
{


    /*
     * 查询用户绑定数据
     * Created By caicm
     * @param int $member_id 用户id
     * @param string $group_id 组名id
     * return mixed
     */
    public function finGroupUser($member_id, $group_id){

        $where = array(
            'louxiaoer_user_id' => $member_id,
            'group_id' => $group_id
        );

        return $this->where($where)->find();

    }

    /*
     * 绑定会员到群组
     * Created By caicm
     * @param $member_id 用户id
     * @param int $group_id 群组id
     * return mixed
     */
    public function bindingUserToGroup($member_id, $group_id){

        $data = array(
            'louxiaoer_user_id' => $member_id,
            'group_id' => $group_id,
            'add_time' => gmtime()
        );

        return $this->data($data)->add();

    }

    /*
     * 删除群组用户
     * Created By caicm
     * @param $member_id 用户id
     * @param int $group_id 群组id
     * return mixed
     */
    public function delUserToGroup($member_id=0, $group_id){
        $where = array(
            'louxiaoer_user_id' => $member_id,
            'group_id' => $group_id
        );

        if($member_id==0) unset($where['louxiaoer_user_id']);

        return $this->where($where)->delete();

    }

    /*
     * 获取群组用户人数
     * Created By caicm
     * @param int $group_id 群组id
     * return number
     */
    public function findCountUser($group_id, $louxiaoer_id){
        $where = array(
            'lg.group_id' => $group_id,
            'lu.louxiaoer_id' =>$louxiaoer_id
        );

        return $this->alias('lg')
            ->join('left join sdx_louxiaoer_user as lu on lg.louxiaoer_user_id=lu.id')
            ->where($where)
            ->count('lg.id');

    }

    /*
     * 群组列表
     * Created By caicm
     * @param int $member_id
     * return number
     */
    public function get_group_list($member_id){

        $where = array(
            'u.louxiaoer_user_id' => $member_id
        );

        $field = "g.group_name";

        return $this
            ->alias('u')
            ->field($field)
            ->join('left join sdx_louxiaoer_group as g on u.group_id=g.group_id')
            ->where($where)
            ->select();

    }







}